package cn.tedu.tmall.front.account.dao.persist.repository.impl;

import cn.tedu.tmall.front.account.dao.persist.mapper.UserMapper;
import cn.tedu.tmall.front.account.dao.persist.repository.IUserRepository;
import cn.tedu.tmall.front.account.pojo.entity.User;
import cn.tedu.tmall.front.account.pojo.vo.UserStandardVO;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Repository;

@Repository
@Slf4j
public class UserRepository implements IUserRepository {
    @Autowired
    private UserMapper userMapper;

    @Override
    public int updateById(User user) {
        log.debug("开始执行根据用户id修改用户信息");
        return userMapper.updateById(user);
    }

    @Override
    public int countByUsername(String username) {
        log.debug("查看用户名是否存在"+username);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("username",username);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByPhone(String phone) {
        log.debug("查看手机号是否存在"+phone);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByPhoneAndNotId(String phone, Long userId) {
        log.debug("开始处理手机号存在但是用户名不匹配"+phone+userId);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("phone",phone);
        queryWrapper.ne("id",userId);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByEmail(String email) {
        log.debug("查询邮箱号是否存在");
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",email);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public int countByEmailAndNotId(String email, Long userId) {
        log.debug("开始处理邮箱号存在但是用户名不匹配"+email+userId);
        QueryWrapper<User> queryWrapper=new QueryWrapper<>();
        queryWrapper.eq("email",email);
        queryWrapper.ne("id",userId);
        return userMapper.selectCount(queryWrapper);
    }

    @Override
    public UserStandardVO getStandardById(Long id) {
        log.debug("开始执行根据用户id查询用户信息");
        return userMapper.getStandardById(id);
    }
}
